//
//  BVMineViewController.m
//  BuyVegetable
//
//  Created by yushanchang on 16/9/28.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVMineViewController.h"
#import "BVMyOrderViewController.h"
#import "BVMyWalletViewController.h"
#import "BVCouponViewController.h"
#import "BVVIPCenterViewController.h"
#import "BVMyAddressViewController.h"
#import "BVRecommendViewController.h"
#import "BVServiceNoteViewController.h"
#import "BVFeedbackViewController.h"
#import "BVSettingViewController.h"
#import "BVLoginViewController.h"
#import "UIView+Extension.h"
#import "BVMineTool.h"
#import "BVUserInfoViewController.h"
#import "BVMyCollectionViewController.h"
#import "UIView+TransitionAnimation.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "BVMineHeaderCell.h"
#import "BVSectionOneCell.h"
#import "BVSectionTwoCell.h"

#define MINEHEADER_CELL @"BVMineHeaderCell"
#define SECTIONONE_CELL @"BVSectionOneCell"
#define SECTIONTWO_CELL @"BVSectionTwoCell"



@interface BVMineViewController ()
<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *localArr;
@property (nonatomic, strong) UIView *navBarView;

@end

@implementation BVMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self.tableView reloadData];
}

- (void)setTableView:(UITableView *)tableView {
    _tableView = tableView;
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [tableView registerNib:NIB_NAMED(MINEHEADER_CELL) forCellReuseIdentifier:MINEHEADER_CELL];
    [tableView registerNib:NIB_NAMED(SECTIONONE_CELL) forCellReuseIdentifier:SECTIONONE_CELL];
    [tableView registerNib:NIB_NAMED(SECTIONTWO_CELL) forCellReuseIdentifier:SECTIONTWO_CELL];
//            UIView *navBarView = [[UIView alloc] init];
//            [navBarView setAlpha:0];
//            [navBarView setFrame:CGRectMake(0, 0, ScreenW, 64)];
//            [navBarView setBackgroundColor:[UIColor BVBaseOrangeColor]];
//            self.navBarView = navBarView;
//    
//            UILabel *titleLabel = [[UILabel alloc] init];
//            [titleLabel setBackgroundColor:[UIColor clearColor]];
//            [titleLabel setText:@"我的"];
//            [titleLabel setTextColor:[UIColor whiteColor]];
//            [titleLabel setTextAlignment:NSTextAlignmentCenter];
//            [titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
//            [titleLabel setWidth:50];
//            [titleLabel setHeight:25];
//            [titleLabel setCenter:CGPointMake(navBarView.center.x, navBarView.center.y + 10)];
//    
//            [navBarView addSubview:titleLabel];
//            [self.view.superview addSubview:navBarView];
}

#pragma mark -
#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 220;
    } else if (indexPath.section == 1) {
        return 100;
    } else {
        return 200;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        BVMineHeaderCell *header_cell = [tableView dequeueReusableCellWithIdentifier:MINEHEADER_CELL];
        [header_cell.avatar sd_setImageWithURL:[NSURL URLWithString:self.user.avatar_url] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"icon")];
        NSLog(@"%@",self.user.avatar_url);
        header_cell.clickBlock = ^{
            [self avatarClick];
        };
        return header_cell;

    } else if (indexPath.section == 1) {
        BVSectionOneCell *sectionone_cell = [tableView dequeueReusableCellWithIdentifier:SECTIONONE_CELL];
        for (UIButton *btn in sectionone_cell.oneBtn) {
            [btn addTarget:self action:@selector(oneBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return sectionone_cell;
    } else {
        BVSectionTwoCell *sectiontwo_cell = [tableView dequeueReusableCellWithIdentifier:SECTIONTWO_CELL];
        for (UIButton *btn in sectiontwo_cell.twoBtn) {
            [btn addTarget:self action:@selector(twoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        }

        
        return sectiontwo_cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else if (section == 1) {
        return 5;
    } else {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}

- (void)oneBtnClick:(UIButton *)button {
    if (button.tag == 11) {
        BVMyOrderViewController *myOrder = [[BVMyOrderViewController alloc] init];
        [self.navigationController pushViewController:myOrder animated:YES];
    } else if (button.tag == 12) {
        BVMyCollectionViewController *myCollection = [[BVMyCollectionViewController alloc] init];
        [self.navigationController pushViewController:myCollection animated:YES];
    } else if (button.tag == 13) {
        BVMyAddressViewController *myAddress = [[BVMyAddressViewController alloc] init];
        [self.navigationController pushViewController:myAddress animated:YES];
    } else {
        BVCouponViewController *coupon = [[BVCouponViewController alloc] init];
        [self.navigationController pushViewController:coupon animated:YES];
    }
}

- (void)twoBtnClick:(UIButton *)button {
    if (button.tag == 21) {
        BVMyWalletViewController *myWallet = [[BVMyWalletViewController alloc] init];
        [self.navigationController pushViewController:myWallet animated:YES];
    } else if (button.tag == 22) {
    
    } else if (button.tag == 23) {
        BVVIPCenterViewController *VIP = [[BVVIPCenterViewController alloc] init];
        [self.navigationController pushViewController:VIP animated:YES];
    } else if (button.tag == 24) {
        BVRecommendViewController *recommend = [[BVRecommendViewController alloc] init];
        [self.navigationController pushViewController:recommend animated:YES];
    } else if (button.tag == 25) {
        [self callPhone];
    } else if (button.tag == 26) {
        BVServiceNoteViewController *serviceNote = [[BVServiceNoteViewController alloc] init];
        [self.navigationController pushViewController:serviceNote animated:YES];
    } else if (button.tag == 27) {
        BVFeedbackViewController *feedback = [[BVFeedbackViewController alloc] init];
        feedback.type = 1;
        [self.navigationController pushViewController:feedback animated:YES];
    } else if (button.tag == 28) {
        BVSettingViewController *setting = [[BVSettingViewController alloc] init];
        [self.navigationController pushViewController:setting animated:YES];
    }
}

#pragma mark -
#pragma mark - 打电话
- (void)callPhone {
    NSString *phoneNumber = [@"telprompt://" stringByAppendingString:@"053158192080"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
}

#pragma mark -
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat contentOffSetY = scrollView.contentOffset.y;
    if (contentOffSetY <= 64) {
        self.navBarView.alpha = contentOffSetY / 64;
    } else {
        self.navBarView.alpha = 1.f;
    }
}

#pragma mark -
#pragma mark - 头像点击事件
- (void)avatarClick {
    if (self.user.login) {
        BVUserInfoViewController *userInfo = [[BVUserInfoViewController alloc] init];
        [BVKeyWindow addTransitionAnimationWithDuration:0.5f type:TransitionOglFlip subType:FROM_LEFT];
        [self.navigationController pushViewController:userInfo animated:YES];
    } else {
        BVLoginViewController *login = [[BVLoginViewController alloc] init];
        [BVKeyWindow addTransitionAnimationWithDuration:0.5f type:TransitionOglFlip subType:FROM_LEFT];
        [self.navigationController pushViewController:login animated:YES];
    }
}


@end
